


import formula.calculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Momentum extends Activity{
        
        private double answer = 0;
        private LinearLayout ll;
        private TextView tv;
        
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.momentum_activity);
        ll = (LinearLayout)findViewById(R.id.momentumLayout);
        tv = new TextView(this);
    }
        
        public void onClick(View v) {
                switch(v.getId()) {
                        case R.id.solveMomentum:
                                solveMomentum("momentum");
                                break;
                        case R.id.solveMass:
                                solveMomentum("mass");
                        case R.id.solveVelocity:
                                solveMomentum("velocity");
                                break;
                }
                tv.setText("Answer: " + answer);
                ll.addView(tv);
        }
        
        private void solveMomentum(String str) {
                EditText mass = (EditText) findViewById(R.id.firstMass);
                EditText velocity = (EditText) findViewById(R.id.firstVelocity);
                String sMass = mass.getText().toString();
                String sVelocity = velocity.getText().toString();
                double massNum = 0;
                double velocityNum = 0;
                double secondVelocityNum = 0;
                double secondMassNum = 0;
                
                if(!sMass.equals("")) {
                        massNum = Double.parseDouble(sMass);
                }
                if(!sVelocity.equals("")) {
                        velocityNum = Double.parseDouble(sVelocity);
                }
                
                if(str.equals("momentum")) {
                        momentum(massNum, velocityNum);
                } else if(str.equals("mass")) {
                        EditText secondVelocity = (EditText) findViewById(R.id.secondVelocity);
                        String sSecondVelocity = secondVelocity.getText().toString();
                        if(!sSecondVelocity.equals("")) {
                                secondVelocityNum = Double.parseDouble(sSecondVelocity);
                        }
                        secondMass(massNum, velocityNum, secondVelocityNum);
                } else if(str.equals("velocity")) {
                        EditText secondMass = (EditText) findViewById(R.id.secondMass);
                        String sSecondMass = secondMass.getText().toString();
                        if(!sSecondMass.equals("")) {
                                secondMassNum = Double.parseDouble(sSecondMass);
                        }
                        secondVelocity(massNum, velocityNum, secondMassNum);
                }
        }

        private void momentum(double mass, double velocity) {
                double result = mass * velocity;
                answer = result;
        }
        
        private void secondMass(double firstMass, double firstVelocity, double secondVelocity) {
                double result = firstMass * firstVelocity / secondVelocity;
                answer = result;
        }
        
        private void secondVelocity(double firstMass, double firstVelocity, double secondMass) {
                double result = firstMass * firstVelocity / secondMass;
                answer = result;
        }
}